# -*- coding: utf-8 -*- 

import sys
default_encoding = 'utf-8'

if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

import MySQLdb

class MyDatabaseTmall(object):
    def __init__(self):
        self.conn=conn=conn=MySQLdb.connect(host='115.238.55.68',user='root',passwd='123,abc',db='tmall',port=3306,charset='utf8' )
        #self.conn=conn=conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='08034615',db='tmall',port=3306,charset='utf8' )

        self.cursor = conn.cursor()
        self.sqllist=[]
        self.sqlcount=0
        
    def addsql(self,sql):
        self.sqllist.append(sql)
        self.sqlcount+=1
    
    def runsqllist(self):
        for sql in self.sqllist:
            self.runsqlsolo(sql)
        
    def runsqlsolo(self,sql):
        print sql
        self.cursor.execute(sql)#.decode('utf-8').encode('gbk'))
        self.conn.commit()
        
    def fetchdata(self,sql):
        #print sql
        self.cursor.execute(sql)
        ituple=self.cursor.fetchall()
        return ituple
    
    def getfielddata(self,fieldname_tuple,table):
        sql='select '
        for i in fieldname_tuple:
            sql+=i+','
        #print sql
        sql=sql[:-1]+' from '+table
        #print sql
        ituple=self.fetchdata(sql)#.decode('utf-8').encode('gbk'))
        return ituple
        